package com.hzlx;


import com.hzlx.dto.*;
import jakarta.annotation.Resource;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.Map;

@RestController
public class InventoryController {
    @Resource
    RestTemplate restTemplate;
    @PostMapping("/api/inventory/isInStock")
    @ResponseStatus(HttpStatus.OK)
    public ResultDto<List<InventoryResponse>> isInStockMap(@RequestBody List<Map<String,Object>> skus) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity entity = new HttpEntity<>(skus, headers);
        ParameterizedTypeReference<List<InventoryResponse>> pt = new ParameterizedTypeReference<>() {};
        ResponseEntity<List<InventoryResponse>> exchange =
                restTemplate.exchange("http://inventory-service/api/inventory/isInStock",
                        HttpMethod.POST,
                        entity,
                        pt);
        return ResultDto.<List<InventoryResponse>>builder()
                .code("ok")
                .data(exchange.getBody())
                .build();
    }

    @PostMapping("/api/inventory")
    @ResponseStatus(HttpStatus.CREATED)
    public void create(@RequestBody InventoryRequest inventoryRequest){
        HttpHeaders headers = new HttpHeaders();
        //这是请求参数传递的方式为json
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity entity = new HttpEntity<>(inventoryRequest, headers);
        restTemplate.postForLocation("http://inventory-service/api/inventory",entity);
    }
    @PutMapping("/api/inventory")
    @ResponseStatus(HttpStatus.OK)
    public void  update(@RequestBody InventoryRequest inventoryRequest){
        HttpHeaders headers = new HttpHeaders();
        //这是请求参数传递的方式为json
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity entity = new HttpEntity<>(inventoryRequest, headers);
        restTemplate.put("http://inventory-service/api/inventory",entity);
    }

}
